package com.demo.flight.controller;

//import com.baomidou.mybatisplus.core.conditions.Wrapper;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.flight.config.Result;
import com.demo.flight.config.ResultCodeEnum;
import com.demo.flight.entity.Customer;
import com.demo.flight.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ssy
 * @create 2022-03-23 10:17
 */

@CrossOrigin
@ResponseBody
@Controller
@RequestMapping("/customer")
public class CustomerController {
    
    @Autowired
    private CustomerService customerService;
    
    @GetMapping("/getAll")
    public Result getAllCustomersByCondition(Customer customer) {
        List<Customer> customerList;
        if (customer == null) {
            customerList = customerService.getAll();
        } else {
            customerList = customerService.getAllByCondition(customer);
        }
        return Result.success(customerList, ResultCodeEnum.SUCCESS);
    }
    
    @PostMapping("/add")
    public Result addCustomer(Customer customer) {
        if (customer == null) {
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        if (customerService.add(customer)) {
            return Result.success();
        }
        return Result.error(ResultCodeEnum.USER_EXIST_ERROR);
    }
    
    @PutMapping("/update")
    public Result updateCustomer(Customer customer) {
        if (customer == null) {
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        if (customerService.update(customer)) {
            return Result.success();
        }
        return Result.error(ResultCodeEnum.USER_NOT_EXIST_ERROR);
    }
    
    @DeleteMapping("delete")
    public Result deleteCustomer(Integer customerId) {
        if (customerId == null) {
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        if (customerService.delete(customerId)) {
            return Result.success();
        }
        return Result.error(ResultCodeEnum.USER_NOT_EXIST_ERROR);
    }
    
}
